import { NextRequest } from "next/server";
import { comments } from "./data";

export async function GET(request: NextRequest) {
    // 获取查询参数
    const { searchParams } = request.nextUrl;
    const query = searchParams.get("query");
    // 如果有查询参数，则过滤评论
    const filerComments = query ? comments.filter(comment => comment.text.includes(query)) : comments;
    // 返回响应
    return Response.json(filerComments);
}

export async function POST(request: Request) {
    // 获取Request的内容
    const comment = await request.json();
    const newComment = {
        id: comments.length + 1,
        text: comment.text
    };
    comments.push(newComment);
    // 返回响应
    return new Response(JSON.stringify(comment), {
        headers: { "Content-type": "application/json" },
        status: 201
    })
}